package com.gbcom.autocompile;


import java.util.Date;

import org.apache.log4j.Logger;

import com.gbcom.autocompile.prop.SysProperies;
import com.gbcom.autocompile.scheduler.JobWrapper;
import com.gbcom.autocompile.scheduler.Scheduler;
import com.gbcom.op.scheduler.util.JobException;
/**
 * 入口类。。。
 * 设计原则，将构建过程风味  编译和 容器运行两个步骤
 *   1.编译：脚本  compiler-xxx.bat  功能包括：checkout  build jar  copy
 *   2.容器运行 脚本 restart-xxx.bat 功能包括：关闭容器和重启
 *   
 *   程序负责将流程统一 并邮件通知等都能、
 *   
 * @author Administrator
 *
 */
public class AutoCompile {
	private static Logger logger = Logger.getLogger(AutoCompile.class);
	public static int compiler_method = 1; //ant
	public static void main(String args []){
		try {
			int w = Integer.valueOf(args[0]);
			compiler_method = Integer.valueOf(args[1]);//1 :ant 2:mvn
			AutoCompile autoCompiler = new AutoCompile();
			logger.info("AutoCompile start -- Run-mode : "+ w +" (1:once ; 2:schedule ;3 :all)   -  compiler-method:"+compiler_method+"(1:ant ; 2:mvn)  ;; "+new Date());
			switch(w){
			case 1:
				autoCompiler.once();
				break;
			case 2:
				autoCompiler.schedule();
				break;
			case 3:
				autoCompiler.once();
				autoCompiler.schedule();
				break;
			default :
				logger.error("valid value --- 1: once 2:schedule 3： all  ");
			}
		} catch (Exception e) {
//			new AutoCompile().schedule();
			logger.error("init failed!!!!!,the job can not excute!!!");
		}
	}
	
	private void schedule(){

		logger.info("start auto compile schedule,,,time="+new Date());
		JobWrapper jobWrapper = new JobWrapper("AutoCompile","Compile",BatRunJob.class);
		jobWrapper.addCronTrigger("TriggerGroup1", "trigger1", null, null, SysProperies.getValue("quartz.conn"));
		try {
			Scheduler.getInstance().register(jobWrapper);
		} catch (JobException e) {
			logger.error("register job failed!! the compile job is not run!!!");
		}
	
	}
	
	public void once( ){
		logger.info("start auto compile once,,,time="+new Date());
		try {
			BatRunJob job = new BatRunJob();
			job.doTask();
		} catch (Exception e) {
			logger.info("start auto compile once failed,,,time="+new Date());
		}
	}
	
}
